<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->
	
<cfcomponent output="false">
	
  	<cffunction name="queryToXML" access="public" returntype="xml" output="false" hint="Return xml">
		<cfargument name="query" type="query" required="yes" />
		<cfargument name="root" type="string" required="no" default="recordset" />
		<cfargument name="elm" type="string" required="no" default="row" />
		<cfscript>
		var myQuery		= arguments.query ;
		var result		= XmlNew();
		var colCount	= 0 ;
		var colName		= arrayNew(1) ;
		var metaData	= "" ;
		var q	=	0 ;
		var i	=	0 ;
		
		metaData	=	myQuery.getMetaData() ;
		colCount	=	metadata.getColumnCount() ;
		colName	 	=	metadata.getColumnLabels() ;
			
		result.xmlRoot = XmlElemNew( result , arguments.root );				
		
		for( q = 1 ; q LTE myQuery.recordCount ; q = q + 1 ) {

			result.recordset.XmlChildren[q] = XmlElemNew( result , arguments.elm );
		
			for( i = 1 ; i LTE colCount ; i = i + 1 ) {
				
				result.recordset.row[q].XmlChildren[i] = XmlElemNew( result , colName[ i ] ) ;
				result.recordset.row[q].XmlChildren[i].XmlText = myQuery[colName[ i ]][ q ] ;
		
			}
		
		}
		</cfscript>
		<cfreturn result />
	</cffunction>

	<cffunction name="queryToArray" access="public" returntype="array" output="false" hint="Return array of struct" >
		<cfargument name="query" type="query" required="yes" />
		<cfscript>
		var myArray		=	arrayNew(1);
		var myStruct	=	structNew();
		var myQuery		=	arguments.query;
		var colCount	=	0;
		var colName		=	arrayNew(1);
		var metaData	=	"";
		var q	=	0;
		var i	=	0;
		
		colCount	=	listLen( myquery.columnList ) ;
		colName	 	=	listToArray( myquery.columnList ) ;
		
		for( q = 1 ; q LTE myQuery.recordCount ; q = q + 1 ) {
		
			for( i = 1 ; i LTE colCount ; i = i + 1 ) {
			
				myStruct[ lCase( colName[ i ] ) ] = myQuery[colName[ i ]][ q ] ;
		
			}
		
			myArray[ q ] = myStruct ;
			myStruct = structNew() ;
		
		}
		</cfscript>
		<cfreturn myArray />
	</cffunction>
	
	<cffunction name="appendData" access="public" returntype="array">
		<cfargument name="array" type="Array" required="yes" />
		<cfscript>
		var iteratorData = arguments.array.iterator();
		var iteratorTemp = "";
		var temp = arrayNew(1);
		var result = arrayNew(1);
		
		while( iteratorData.hasNext() ){
		
			temp = queryToArray( iteratorData.next() );
			
			iteratorTemp = temp.iterator();
			
			while( iteratorTemp.hasNext() ){
			
				result.add( iteratorTemp.next() );
				
			
			}
		
		}
		
		return result;
		</cfscript>
	</cffunction>
	
</cfcomponent>